package com.zhouyu.service;

import com.zhouyu.interfacer.faceATest;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author 周瑜
 */

@Component
public class UserService implements InitializingBean {

	@Autowired
	private OrderService orderService;

	@Autowired
	private JdbcTemplate jdbcTemplate;

	//初始化前调用， 这个方法是这个类实现了 InitializingBean 接口
	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("UserService ==初始化前调用 InitializingBean ===");
	}

	//初始化前调用， 是因为添加了 @PostConstruct 注解
	@PostConstruct
	public void a() {
		int b = orderService.b;
		System.out.println(b);
		System.out.println("UserService ==初始化前调用 @PostConstruct ===");
	}

	public void test() {
		System.out.println("test");
	}


	@Transactional
	public void testJdbc() {
		List<Map<String, Object>> maps = jdbcTemplate.queryForList("SELECT * FROM chat_summary WHERE id = '9'");
		System.out.println(maps.toArray());

		//在 AppConfig中配置 @Configuration 注解， 防止事务开启不生效
		testJdbcA();
		throw new NullPointerException();
	}

	@Transactional(propagation = Propagation.NEVER)  //Propagation.NEVER 表示当前要是有事务存在，就抛出一个异常
	public void testJdbcA () {
		System.out.println("testJdbcA");
	}

}
